<#assign path = request.getContextPath() />
<!DOCTYPE html>
<html class="x-admin-sm">
    
    <head>
        <meta charset="UTF-8">
        <title>用户管理</title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
        <#--<#assign path="${springMacroRequestContext.getContextPath()}">-->
        <link rel="stylesheet" href="${path}/static/css/font.css">
        <link rel="stylesheet" href="${path}/static/css/css.css">
		<link rel="stylesheet" type="text/css" href="${path}/static/css/xadmin.css"/>
        <script src="${path}/static/lib/layui/layui.js" charset="utf-8"></script>
        <script type="text/javascript" src="${path}/static/js/jquery.min.js"></script>
        <script src="${path}/static/js/xadmin.js" type="text/javascript"></script>
    </head>
    
    <body>
        <div class="x-nav">
            
                <a href="../index">首页</a>
				<span>></span>
                <a><cite>人员管理</cite></a>
        </div>
        <div class="layui-fluid">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md12">
					<div id="btns" class="btns"> 
						<button type="button" class="btn" onclick="batchImportInfo()">批量导入</button>
						<button type="button" class="btn" onclick="singleAdd()">单个新增</button>
						<button type="button" class="btn" onclick="changeDept()">调整部门</button>
						<button type="button" class="btn" onclick="batchLoadPic()">批量传照片</button>
						<button type="button" class="btn" onclick="leave()">离职</button>
						<button type="button" class="btn" onclick="overHoliday()">结束休假</button>
						<button type="button" class="btn" onclick="exportInfo()">导出</button>
					</div>
                    <div class="layui-card">
                        <div class="layui-card-body ">
                            <div class="layui-form layui-col-space5">
                            <#--<form class="layui-form layui-col-space5">-->
								<div class="layui-input-inline layui-show-xs-block">
									<input type="text" name="username" id="emp_name" placeholder="姓名" class="layui-input" maxlength="15">
								</div>
								
                                <div class="layui-input-inline layui-show-xs-block">
                                    <select name="contrller" id = "dept_id">
                                        <option value="">部门</option>
                                        <option value="0">开发</option>
                                        <option value="1">产品</option>
                                        <option value="2">财务</option></select>
                                </div>
                                <div class="layui-input-inline layui-show-xs-block">
                                    <select name="contrller" id="emp_state">
                                        <option value="">在职状态</option>
                                        <option value="1">在职</option>
                                        <option value="2">休职</option>
                                        <option value="3">离职</option>
									</select>
                                </div>
								<div class="layui-input-inline layui-show-xs-block">
									<input type="text" name="username" id="emp_no" placeholder="员工编号" class="layui-input" maxlength="15">
								</div>	
								<div class="layui-input-inline ">
									<input type="text" type="number" id="emp_mobile" maxlength="11" class="layui-input" placeholder="手机号">
								</div>
                                
                                <div class="layui-input-inline layui-show-xs-block">
                                    <button class="layui-btn"  onclick="search()">
                                        <i class="layui-icon">&#xe615;</i></button>
                                </div>
                            </div>
                            <#--</form>-->
                        </div>
                        <div class="layui-card-body">
                            <table class="layui-table layui-form">
                                <thead>
                                <tr>
                                    <th>
                                        <input type="checkbox" name="" lay-skin="primary">
                                    </th>
                                    <th>姓名</th>
                                    <th>性别</th>
                                    <th>部门</th>
                                    <th>岗位</th>
                                    <th>工种</th>
                                    <th>员工编号</th>
                                    <th>入职时间</th>
                                    <th>工龄</th>
                                    <th>状态</th>
                                    <th>手机号</th>
                                    <th>身份证号</th>
                                    <th>操作</th>
                                </tr>
                                </thead>
                                <tbody>
                                <#if datas?? && (datas?size > 0)>
                                    <#list datas as item>
						        <tr>
                                    <td><input type="checkbox" name="" lay-skin="primary" value="${item.id}"/></td>
                                    <td>${item.name}</td>
                                    <td>
									    <#if item.gender == 1>
                                            男
                                    <#elseif item.gender == 2>
										    女
                                    </#if>
                                    </td>
                                    <td>${item.deptName}</td>
                                    <td>${item.postName}</td>
                                    <td>${item.jobName}</td>
                                    <td>${item.empNo}</td>
                                    <td>
                                        <#if item.hireDate ??>
                                            ${item.hireDate?string("yyyy-MM-dd")}
                                        </#if>
                                    </td>
                                    <td>${item.workAge}</td>
                                    <td class="td-manage">
									    <#if item.status == 1>
                                            在职
                                        <#elseif item.status == 2>
										    休假
                                        <#elseif item.status == 3>
										    离职
                                        </#if>
                                    </td>
                                    <td>${item.mobile}</td>
                                    <td>${item.identityNo}</td>
                                    <td>
										<#if item.state == 1>
                                            <a title="禁用" id="${item.id}" href="javascript:;" disabled="disabled" onclick="changeEmpState(this)">
                                                <i class="layui-icon">&#xe63c;</i></a>
                                        <#elseif item.state == 2>
											<a title="启用" id="${item.id}" href="javascript:;" disabled="disabled" onclick="changeEmpState(this)">
                                                <i class="layui-icon">&#xe63c;</i></a>
                                        </#if>
                                        <a title="编辑" onclick="editEmployee(this, '${item.id}')" href="javascript:;">
                                            <i class="layui-icon">&#xe63c;</i></a>
                                        <a title="删除" onclick="member_del(this,'要删除的id')" href="javascript:;">
                                            <i class="layui-icon">&#xe640;</i></a>

                                    </td>
                                </tr>
                                    </#list>
                                </#if>
                                </tbody>
                            </table>
                        </div>

                        <div class="layui-table-page">
                            <div id="layui-table-page1">
                                <div class="layui-box layui-laypage layui-laypage-default" id="layui-laypage-3">
                                    <a href="javascript:;" class="layui-laypage-prev layui-disabled"
                                       data-page="0"><i class="layui-icon"></i>
                                    </a>
                                    <span class="layui-laypage-curr">
										<em class="layui-laypage-em"></em>
										<em>1</em>
									</span>
                                    <a href="javascript:;" data-page="2">2</a>
                                    <a href="javascript:;" data-page="3">3</a>
                                    <span class="layui-laypage-spr">…</span>
                                    <a href="javascript:;" class="layui-laypage-last" title="尾页" data-page="300">300</a>
                                    <a href="javascript:;" class="layui-laypage-next" data-page="2">
                                        <i class="layui-icon"></i>
                                    </a>
                                    <span class="layui-laypage-skip">
										到第 <input type="text" min="1" value="1" class="layui-input">页
										<button type="button" class="layui-laypage-btn">确定</button>
									</span>
                                    <span class="layui-laypage-count">共 300 条</span>
                                    <span class="layui-laypage-limits">
										<select lay-ignore="">
											<option value="10" selected="">10 条/页</option>
											<option value="20">20 条/页</option>
											<option value="30">30 条/页</option>
											<option value="40">40 条/页</option>
											<option value="50">50 条/页</option>
											<option value="100">100 条/页</option>
										</select>
									</span>
                                </div>
                            </div>
                        </div>
                        <div class="layui-card-body ">
                            <div class="page">
                                <div>
                                    <a class="prev" href="">&lt;&lt;</a>
                                    <a class="num" href="">1</a>
                                    <span class="current">2</span>
                                    <a class="num" href="">3</a>
                                    <a class="num" href="">489</a>
                                    <a class="next" href="">&gt;&gt;</a></div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </body>
    <script>
        layui.use(['laydate', 'form'],
        function() {
            var laydate = layui.laydate;

            //执行一个laydate实例
            laydate.render({
                elem: '#start' //指定元素
            });

            //执行一个laydate实例
            laydate.render({
                elem: '#end' //指定元素
            });
        });

        /*用户-停用*/
        function member_stop(obj, id) {
            layer.confirm('确认要停用吗？',
            function(index) {

                if ($(obj).attr('title') == '启用') {

                    //发异步把用户状态进行更改
                    $(obj).attr('title', '停用');
                    $(obj).find('i').html('&#xe62f;');

                    $(obj).parents("tr").find(".td-status").find('span').addClass('layui-btn-disabled').html('已停用');
                    layer.msg('已停用!', {
                        icon: 5,
                        time: 1000
                    });

                } else {
                    $(obj).attr('title', '启用');
                    $(obj).find('i').html('&#xe601;');

                    $(obj).parents("tr").find(".td-status").find('span').removeClass('layui-btn-disabled').html('已启用');
                    layer.msg('已启用!', {
                        icon: 5,
                        time: 1000
                    });
                }

            });
        }

        /*用户-删除*/
        function member_del(obj, id) {
            layer.confirm('确认要删除吗？',
            function(index) {
                //发异步删除数据
                $(obj).parents("tr").remove();
                layer.msg('已删除!', {
                    icon: 1,
                    time: 1000
                });
            });
        }

        function delAll(argument) {

            var data = tableCheck.getData();

            layer.confirm('确认要删除吗？' + data,
            function(index) {
                //捉到所有被选中的，发异步进行删除
                layer.msg('删除成功', {
                    icon: 1
                });
                $(".layui-form-checked").not('.header').parents('tr').remove();
            });
        }

        function search() {
            var empName = $('#emp_name').val();
            var deptId = $('#dept_id').val();
            var status = $('#emp_state').val();
            var empNo = $('#emp_no').val();
            var mobile = $('#emp_mobile').val();
            // 查询
            $.ajax({
                url: "/api/employee/info",
                type: "GET",
                data: {
                    name: empName,
                    mobile: mobile,
                    deptId: deptId,
                    empNo: empNo,
                    identityNo: "",
                    status: status,
                    pageSize: 10,
                    page: 1
                },
                success: function (data) {
                    console.log(data);
                    var dataMap = eval('(' + data.datas + ')');
                    var list = dataMap.datas;
                    var html = '';
                    for (var i in list) {
                        var item = list[i];
                        var gender = '';
                        var status = '';
                        var state = '';
                        if (item.gender == 1) {
                            gender = "男";
                        } else {
                            gender = "女";
                        }
                        if (item.status == 1) {
                            status = "在职";
                        } else if (item.status == 2) {
                            status = "休假";
                        } else {
                            status = "离职";
                        }

                        if (item.state == 1) {
                            state = '<a title="禁用" id="'+item.id+'" onclick="changeEmpState(this)" href="javascript:;" disabled="disabled">\n' +
                                    '<i class="layui-icon">&#xe63c;</i></a>';
                        } else {
                            state = '<a title="启用" id="'+item.id+'" href="javascript:;" onclick="changeEmpState(this)" disabled="disabled">\n' +
                                    '<i class="layui-icon">&#xe63c;</i></a>';
                        }
                        var time = getDate(item.hireDate);

                        html += '<tr>' +
                                '<td><input type="checkbox" name="" lay-skin="primary" value="'+item.id+'">' +
                                '</td>' +
                                '<td>'+item.name+'</td>' +
                                '<td>'+gender+'</td>' +
                                '<td>'+item.deptName+'</td>' +
                                '<td>'+item.postName+'</td>' +
                                '<td>'+item.jobName+'</td>' +
                                '<td>'+item.empNo+'</td>' +
                                '<td>'+time+'</td>' +
                                '<td>'+item.workAge+'</td>' +
                                '<td>'+status+'</td>' +
                                '<td>'+item.mobile+'</td>' +
                                '<td>'+item.identityNo+'</td>' +
                                '<td>"+state+"' +
                                '<button type="button" class="query tab-bor">编辑</button>\n' +
                                '<a title="删除" onclick="member_del(this,\'要删除的id\')" href="javascript:;">\n' +
                                '<i class="layui-icon">&#xe640;</i></a>' +
                                '</td>'
                        '</tr>';
                    }
                    $('table').find("tbody").html(html);
                }
            });
        };
        //转换年月日方法
        function getDate(str){
            var oDate = new Date(str),
                    oYear = oDate.getFullYear(),
                    oMonth = oDate.getMonth()+1,
                    oDay = oDate.getDate(),
                    oTime = oYear +'-'+ getzf(oMonth) +'-'+ getzf(oDay);//最后拼接时间
            return oTime;
        };
        //补0操作
        function getzf(num){
            if(parseInt(num) < 10){
                num = '0'+num;
            }
            return num;
        };

        // 导出
        function exportInfo() {
            var empName = $('#emp_name').val();
            var deptId = $('#dept_id').val();
            var status = $('#emp_state').val();
            var empNo = $('#emp_no').val();
            var mobile = $('#emp_mobile').val();
            window.open("/api/employee/info/export?name="+empName+"&mobile="+mobile+"&deptId="+deptId+"&empNo="+empNo+"&status="+status,"_self");
        }
        // 批量导入
        function batchImportInfo() {
            location.href="/userManage/batchImport";
        }

        // 单个新增
        function singleAdd() {
            // location.href="/userManage/add"
            layer.open({
                type: 2,
                title: ['新增员工', 'font-size:10'],
                shadeClose: false,
                area: ['500px', '600px'],
                content: '/userManage/add',
                resize: false,
                shade: 0.5,
                maxmin: true,
                btn: ['关闭'],
                btn2:function(index, layero) {
                    layer.closeAll();
                }
            });
        }

        // 调整部门
        function changeDept() {
            location.href="/userManage/changeDept";
        }
        // 批量上传照片
        function batchLoadPic() {
            location.href="/userManage/batchLoadPic";
        }
        // 离职
        function leave() {
            var ids = [];
            $('tbody .layui-form-checked').each(function(){
                var id = $(this).siblings('input').val();
                ids.push(id);
            });
            console.log(ids);
            $.ajax({
                url:'/api/employee/leave',
                type:'POST',
                data:{
                    ids: ids.join(","),
                },
                success:function (res) {
                    console.log(res);
                    if (res.status == 200) {
                        layer.msg("修改成功",{
                            icon:6,
                            time:1000
                        }, function() {
                            location.reload();
                        });
                    }
                }
            });
        }
        // 结束休假
        function overHoliday() {
            var ids = [];
            $('tbody .layui-form-checked').each(function(){
                var id = $(this).siblings('input').val();
                ids.push(id);
            });
            console.log(ids);
            $.ajax({
                url:'/api/employee/working',
                type:'POST',
                data:{
                    ids: ids.join(","),
                },
                success:function (res) {
                    console.log(res);
                    if (res.status == 200) {
                        layer.msg("修改成功",{
                            icon:6,
                            time:1000
                        }, function() {
                            location.reload();
                        });
                    }
                }
            });
        }

        //修改状态
        function changeEmpState(obj) {
            var state = $(obj).attr('title').trim();
            if (state == '禁用') {
                state = 1;
            } else {
                state = 2;
            }
            var empId = $(obj).attr('id');
            $.ajax({
                url:'/api/employee/info/state',
                type:'POST',
                data:{
                    empId: empId,
                    state: state,
                },
                success: function (res) {
                    console.log(res);
                    if (res.status == 200) {
                        var status = res.datas;
                        layer.msg("修改成功", {
                            icon: 6,
                            time:1000,
                        });
                        if (status == 1) {
                            $(obj).attr('title', '禁用');
                        } else {
                            $(obj).attr('title','启用');
                        }
                    }
                }
            });
        }
        //修改信息
        function editEmployee(obj, empId) {
            console.log(empId);
            layer.open({
                type: 2,
                title: ['编辑', 'font-size:10'],
                shadeClose: false,
                area: ['500px', '480px'],
                content: '/userManage/user/edit?id='+empId,
                resize: false,
                shade: 0.5,
                maxmin: true,
                btn: ['关闭'],
                btn2:function(index, layero) {
                    layer.closeAll();
                }
            });
        }
    </script>

</html>